#!/bin/sh
#
### BEGIN INIT INFO
# Provides: st2chatops
# Required-Start: $local_fs $network $named $remote_fs $syslog
# Required-Stop: $local_fs $network $named $remote_fs $syslog
# Short-Description: st2chatops component
# Description: st2chatops - StackStorm service st2chatops
### END INIT INFO

# st2chatops    This shell script takes care of starting and stopping
#             st2chatops daemon.
# chkconfig: - 85 15
# description: st2chatops - StackStorm service st2chatops
# processname: st2chatops

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

DESC="st2chatops"
NAME=st2chatops
DAEMON=/opt/stackstorm/chatops/bin/hubot
DAEMON_ARGS=""
WORKING_DIRECTORY=/opt/stackstorm/chatops
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/st2/$NAME.log
RUNAS_USER="st2"
RUNAS_GROUP="st2"

lockfile=/var/lock/subsys/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 5

# Read configuration variable file if it is present
[ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME
DAEMON_ARGS=""


# Resemble redhatish daemon behaviour but with group and user available
#
nohup_start() {
  local nicelevel corelimit
  local pid base= nice= bg=
  local runas_user="${RUNAS_USER:-root}"
  local runas_group="${RUNAS_GROUP:-root}"
  nicelevel=0

  base=${1##*/}
  __pids_var_run "$base"

  [ -n "$pid" ] && return

  # make sure it doesn't core dump anywhere unless requested
  corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
  # if they set NICELEVEL in /etc/sysconfig/foo, honor it
  [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"

  # Echo daemon
  [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"

  # make sure log file exists and owned by the same user
  touch $LOGFILE && chown $runas_user:$runas_group $LOGFILE

  # And start it up.
  $nice runuser -s /bin/bash -g $runas_group $runas_user -c "$corelimit >/dev/null 2>&1 ; cd $WORKING_DIRECTORY && nohup </dev/null >>$LOGFILE 2>&1 $* &"
}

# Write pid of a running daemon into the pidfile
#
write_pidfile() {
  local currentpid daemon=$1 pidfile=$2 base
  base=${daemon##*/}
  pidfile=${pidfile:-/var/run/$base.pid}

  sleep 0.1
  currentpid=$(ps -A xo pid,cmd | grep "\bbin/$base\b" | tail -n1 | awk '{print $1}')
  [ -n "$currentpid" ] && echo $currentpid > $pidfile
}

start() {
    echo -n $"Starting $NAME: "
    nohup_start $DAEMON $DAEMON_ARGS
    write_pidfile $DAEMON $PIDFILE
    rh_status_q
    retval=$?
    [ $retval -eq 0 ] && success $"$NAME startup" || failure $"$NAME startup"
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $NAME: "
    killproc -p $PIDFILE $DAEMON
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $NAME
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
